//
// Created by Administrator on 2021/12/14.
//

#include "cdb_pool.hpp"
#include "cdb_connect.hpp"
#include <glog/logging.h>
cdb_pool::cdb_pool()
{

}

cdb_pool::~cdb_pool()
{

}

cdb_pool::cdb_pool(std::string pool_name, std::string db_server_ip, unsigned int db_server_port, std::string username,
                   std::string password, std::string db_name,int max_connect)
{
   this->m_pool_name = pool_name;
   this->m_server_ip = db_server_ip;
   this->m_server_port = db_server_port;
   this->m_username = username;
   this->m_password = password;
   this->m_db_name = db_name;
   this->m_db_max_connect = max_connect;
   this->m_db_cur_connect = 2;
    VLOG(0)<<this->m_pool_name<<" this->m_server_ip "<<this->m_server_ip<<" m_server_port "<<m_server_port<<"     this->m_username"<< this->m_username;
}

void cdb_pool::init()
{
    for (int i = 0; i < m_db_cur_connect; ++i)  // 当前工作连接数
    {
        std::shared_ptr<cdb_pool> temp;
        temp.reset(this);
        // 初始化智能指针，多线程下安全传递this指针
        cdb_connect* p_connect = new cdb_connect(temp);
        p_connect->init();
    }
}

std::string cdb_pool::get_server_ip()
{
    return this->m_server_ip;
}

std::string cdb_pool::get_pool_name()
{
    return this->m_pool_name;
}

std::string cdb_pool::get_dbname()
{
    return this->m_db_name;
}

std::string cdb_pool::get_username()
{
    return this->m_username;
}

unsigned int cdb_pool::get_server_port()
{
    return m_server_port;
}

int cdb_pool::get_max_connect()
{
    return this->m_db_max_connect;
}



std::string cdb_pool::get_password()
{
    return this->m_password;
}

cdb_pool *cdb_pool::getInstance(std::string pool_name, std::string db_server_ip,
                                unsigned int db_server_port, std::string username,
                                std::string password, std::string db_name, int max_connect)
{
    static cdb_pool* m_pool = new cdb_pool(pool_name,db_server_ip,db_server_port,
                                         username,password,db_name,max_connect);

    return m_pool;
}
